package cn.tom.tools;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.*;

import java.util.Random;

public class Sample {
    private static Credential cred = new Credential("AKIDAGKKE1eAnXCnT4eH0b6d6qVMH745AyeF", "cPB8zhZP9s4HNZyT4nq3RqIlOE3nVBpT");
    private static HttpProfile httpProfile = new HttpProfile();
    private static ClientProfile clientProfile = new ClientProfile();
    private static SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
    public static String[] getCaptcha(String phone)  {
        SendSmsRequest req = null;
        SendSmsResponse resp = null;
        try {
            httpProfile.setEndpoint("sms.tencentcloudapi.com");
            clientProfile.setHttpProfile(httpProfile);
            req = setSendSmsRequest(phone);
            resp = client.SendSms(req);
        }catch (Exception e){
            System.out.println(e);
        }
        SendStatus[] sendStatusSet = resp.getSendStatusSet();
        String code = sendStatusSet[0].getCode();
        if("OK".equals(code)){
            return req.getTemplateParamSet();
        }
        return null;
    }


    public static SendSmsRequest setSendSmsRequest(String phone){
        Random random = new Random();
        SendSmsRequest req = new SendSmsRequest();
        // 前端拼接 ’+86‘ 上来 ， 或者控制层 拼接
        String[] phoneNumberSet1 = {phone};
        req.setPhoneNumberSet(phoneNumberSet1);
        req.setSmsSdkAppId("1400885937");
        req.setSignName("枝昻张的公众号");
        req.setTemplateId("2060824");
        String s = "";
        for (int i = 0; i < 6; i++) {
            int n = random.nextInt(10);
            s = s + n;
        }
        String[] captchar = {s};
        req.setTemplateParamSet(captchar);
        return req;
    }

}
